import { CCObject, isValid, math, Node } from "cc";

export interface IPos {
    left?: number;
    right?: number;
    top?: number;
    bottom?: number;
    centerX?: number;
    centerY?: number;
}

export enum EType {
    // * @param type 1矩阵；2横向；3竖向；4单格子
    rect = 1,
    horizontal,
    vertical,
    grid,
}
export class WXCustomAd {

    /**原生模板广告矩阵样式id	 */
    private static _customc_rect_adunit = ['adunit-701ed4bc66e84f53'];//进化恐龙
    // private static _customc_rect_adunit = ['adunit-448cd027d4f46e5b'];//变异恐龙

    /**原生模板广告横向样式id	 */
    private static _customc_h_adunit = ['adunit-8b3497c008472733'];//进化恐龙
    // private static _customc_h_adunit = ['adunit-ab35fdbe32c82333'];//变异恐龙

    /**原生模板广告竖向样式id	 */
    private static _customc_v_adunit = ['adunit-2b06c8d97b910c59', 'adunit-0ca4262e49173ffe'];//进化恐龙
    // private static _customc_v_adunit = ['adunit-514e528fdb14c337', 'adunit-e44f1b8d25ffdd94'];//变异恐龙

    /**原生模板广告单个样式id	 */
    private static _customc_one_adunit = ['adunit-37784cc7ebb9dd73'];

    /**广告缓存 */
    private static _ads = {};

    //隐藏格子广告
    private static hideGridAd = false;

    /** */
    private static _waits = [];

    private static sysInfo = null;
    /**
     * 设置广告id
     * @param rectid 矩阵样式id
     * @param hid 横向样式id
     * @param vid 竖向样式id
     * @param oneid 单格子样式id
     */
    static setadunit(rectid, hid, vid, oneid) {
        this._customc_rect_adunit = rectid;
        this._customc_h_adunit = hid;
        this._customc_v_adunit = vid;
        this._customc_one_adunit = oneid;
    }

    /**
     * 创建一个原生广告对象
     * @param flag 创建来源标识（用标识来控制显示和隐藏）
     * @param type 1矩阵；2横向；3竖向；4单格子
     * @param pos 位置(对象可包含left、right、top、bottom、centerX、centerY字段)。分别表示距离左右上下和中心点的距离
     * @returns 
     */
    static createCustomAd(flag, type: EType, pos: IPos, extraId?: any) {
        if (!window.wx) return;
        this.hideGridAd = false;
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync()
        }
        let version = this.sysInfo.SDKVersion;
        let ad = null;
        let id = "";
        let self = this;

        if (type == 1) {
            id = this._customc_rect_adunit[this.rand(0, this._customc_rect_adunit.length - 1)];
        } else if (type == 2) {
            id = this._customc_h_adunit[this.rand(0, this._customc_h_adunit.length - 1)];
        } else if (type == 3) {
            id = this._customc_v_adunit[this.rand(0, this._customc_v_adunit.length - 1)];
        }
        else if (type == 4) {
            id = this._customc_one_adunit[this.rand(0, this._customc_one_adunit.length - 1)];
        }
        if (extraId) {
            if (typeof extraId == "string") {
                id = extraId;
            } else if (extraId instanceof Array) {
                id = extraId[this.rand(0, extraId.length - 1)];
            }

        }

        //id不存在
        if (id == "") return;

        console.log("显示原生模板广告", flag, this._ads, this._waits);
        //缓存有
        if (this._ads[flag]) {
            ad = this._ads[flag];
            console.log("缓存有");
            if (!ad.isShow()) ad.show();
        }
        else if (this.compareVersion(version, '2.11.1') >= 0) {
            console.log("创建原生模板广告" + flag, "id:" + id, "pos:", pos);
            let p = self.getPos(type, pos);
            ad = wx.createCustomAd({
                adUnitId: id,
                adIntervals: 30,
                style: {
                    top: p.top,
                    left: p.left,
                    width: 330, // 用于设置组件宽度，只有部分模板才支持，如矩阵格子模板
                    fixed: false // fixed 只适用于小程序环境
                }
            })
            if (this.adonload) ad.offLoad(this.adonload);
            this.adonload = () => {
                self._ads[flag] = ad;
                let index = self._waits.indexOf(flag);
                console.log("原生模板广告加载完成：" + flag, self._waits, index);
                if (index == -1 && !this.hideGridAd)
                    ad.show();
                else {
                    self._waits = self._waits.filter(x => x != flag);
                    // self._waits.splice(index, 1);
                    ad.hide().then(() => {

                    }).catch(() => {
                        // self.hideCustomAd(flag);
                        console.log("第二次hide");
                        ad.hide().catch(() => {
                            console.log("第三次hide");
                        });
                    });
                }

            };
            ad.onLoad(this.adonload);

            this.adonError = (res) => {
                console.log('原生模板广告加载失败:', res.errCode, ' ', res.errMsg);
            };
            if (this.adonError) ad.offError(this.adonError);
            ad.onError(this.adonError);

            if (this.adonClose) ad.offClose(this.adonClose);
            this.adonClose = (res) => {
                console.log("原生模板广告关闭");
            };
            ad.onClose(this.adonClose);
        } else {
            // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
            // wx.showModal({
            //     title: '提示',
            //     content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            // })
        }
    }

    public static rand(min, max) {
        return min + Math.floor(Math.random() * (max - min + 1));
    }
    /**
     * 隐藏原生模板广告
     * @param flag 
     */
    public static hideCustomAd(flag) {
        this.hideGridAd = true;
        let ad = null;

        console.log("隐藏原生模板广告", flag, this._ads, this._waits);
        if (this._ads[flag]) {
            ad = this._ads[flag];
            if (ad.isShow())
                ad.hide().then(() => {

                }).catch(() => {
                    // self.hideCustomAd(flag);
                    console.log("第二次hide");
                    ad.hide().catch(() => {
                        console.log("第三次hide");
                    });
                });
        } else {
            this._waits.push(flag);
        }
    }
    public static destroyCustomAd(flag) {
        if (this._ads[flag]) {

            this._ads[flag].onLoad(() => { });
            this._ads[flag].onError(() => { });
            this._ads[flag].onClose(() => { });
            this._ads[flag].destroy();
            this._ads[flag] = null;
        }
    }

    private static compareVersion(v1, v2) {
        v1 = v1.split('.')
        v2 = v2.split('.')
        const len = Math.max(v1.length, v2.length)

        while (v1.length < len) {
            v1.push('0')
        }
        while (v2.length < len) {
            v2.push('0')
        }

        for (let i = 0; i < len; i++) {
            const num1 = parseInt(v1[i])
            const num2 = parseInt(v2[i])

            if (num1 > num2) {
                return 1
            } else if (num1 < num2) {
                return -1
            }
        }

        return 0
    }

    /**
     * 获取实际的位置
     * @param type 模板类型
     * @param pos 游戏中的相对位置
     * @returns 
     */
    private static getPos(type, pos: IPos) {
        if (!this.sysInfo) {
            this.sysInfo = wx.getSystemInfoSync();
        }
        let sysInfo = this.sysInfo;
        let w = 0, h = 0;

        if (type == 1) { w = 330; h = 360; };
        if (type == 2) { w = 360; h = 106; };
        if (type == 3) { w = 72; h = 250; };
        if (type == 4) { w = 68; h = 106; };


        // if (type == 1) { w = 330; h = 360; };
        // if (type == 2) { w = 72; h = 250; };
        // if (type == 3) { w = 72; h = 250; };
        // if (type == 4) { w = 68; h = 106; };


        let sc_w = sysInfo.windowWidth / cc.winSize.width;
        let sc_h = sysInfo.windowHeight / cc.winSize.height;

        let l = (sysInfo.windowWidth - w) / 2;
        let t = (sysInfo.windowHeight - h) / 2;
        if (pos && pos.left != undefined) l = pos.left * sc_w;
        if (pos && pos.top != undefined) t = pos.top * sc_h;
        if (pos && pos.right != undefined) l = sysInfo.windowWidth - w - pos.right * sc_w;
        if (pos && pos.bottom != undefined) t = sysInfo.windowHeight - h - pos.bottom * sc_h;
        if (pos && pos.centerX != undefined) l = (sysInfo.windowWidth - w) / 2 + pos.centerX * sc_w;
        if (pos && pos.centerY != undefined) t = (sysInfo.windowHeight - h) / 2 + pos.centerY * sc_h;

        return {
            left: l,
            top: t
        }
    }
    public static hideAllAd() {
        for (const key in this._ads) {
            this.hideCustomAd(key);
        }
    }
}